layui.use(['form','jquery','table','layer'],function(){
    let form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    //返回上页面
    $('.back_btn').click(function(){
        window.location.href = "views/cust-info.jsp";
    });

    //加载数据
    let contactTable = table.render({
        elem: '#contactList'
        , height: 312
        , url: '/contact/list'
        , page: true
        , cols: [[
             {field: 'id', title: 'ID',width:40}
            ,{field: 'name', title: '姓名'}
            ,{field: 'sex', title: '性别'}
            ,{field: 'job', title: '职位'}
            ,{field: 'email', title: '电子邮箱'}
            ,{field: 'tel', title: '手机'}
            ,{field: 'remark', title: '备注'}
            ,{title: '操作', fixed: 'right', toolbar: "#contactTool"}
            ]]
    });

    ////添加////
    // $('.add_btn').click(function () {
    //     layer.open({
    //         type:2,
    //         content:'views/contact/save.jsp',
    //         skin:'layui-layer-molv',
    //         title:"添加联系人",
    //         area:["650px","500px"],
    //         btn:['确定','关闭'],
    //         btnAlign: 'c',   //按钮居中
    //         yes:function (index,layero) {
    //             let contact_save = layer.getChildFrame('body',index).find("#contact_save");
    //             let data = {};
    //             contact_save.serializeArray().forEach(function(val){
    //                 data[val.name] = val.value;
    //             });
    //             $.post('contact/save',data,function (info) {
    //                 layer.close(index);
    //                 layer.msg(info.msg);
    //                 contactTable.reload();
    //             });
    //         }
    //     });
    // });

    $('.add_btn').click(function () {
        layer.open({
            type:2,
            content:'views/contact/save.jsp',
            skin:'layui-layer-molv',
            title:"添加联系人",
            area:["650px","500px"]
        });
    });

    form.on('submit(saveSubmit)',function(){0
        window.parent.location.reload(); //刷新父页面
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);  // 关闭layer
    });

    $('.saveClose').click(function () {
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    });

    //刷新单选框radio，解决单选框不显示的问题
    layui.use('form',function(){
        var form = layui.form;
        form.render('radio');
    });

    //工具栏：编辑和删除
    table.on('tool(contactList)',function (obj) {
        switch (obj.event){
            case 'edit_btn':
                layer.open({
                    type:2,
                    content:"contact/editPage/"+obj.data.id,
                    skin:'layui-layer-molv',
                    title:"编辑联系人",
                    area:["650px","560px"],
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除['+obj.data.name+']吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function () {
                    $.post('contact/del/'+obj.data.id,function (data) {
                        contactTable.reload();
                        layer.msg(data.msg);
                    });
                });
        }
    });

    //编辑后关闭并刷新页面
    form.on('submit(editSubmit)',function(){
        window.parent.location.reload();
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    $('.editClose').click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    /////////welcome.jsp/////////////
    $('.go_btn').click(function () {
        window.location.href="views/contact/list.jsp";
    });

});